<?php
/*
 * @Author: your name
 * @Date: 2020-06-10 20:13:05
 * @LastEditTime: 2020-06-11 20:16:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hotel\application\api\controller\Usermain.php
 */
namespace app\api\controller;

use app\api\controller\Base;

/**
 * 生成订单
 */
class Orderin extends Base
{

    //储值卡生成订单 
    public function money()
    {
        
        $user_id = $this->user_id;
        $card_id = input('card_id');
        if(!$card_id) return json(['code'=>0,'msg'=>"参数错误"]);

        $card = db('recharge_card')->where(['id'=>$card_id,'status'=>0,'endtime'=>['gt',time()]])->find();
        
        if(empty($card)) return json(['code'=>0,'msg'=>"该充值卡已下架"]);
        if($card['number']<1) return json(['code'=>0,'msg'=>"储值卡库存不足"]);
        
        $data = [
            'user_id' => $user_id,
            'money' => $card['money'],
            'card_id' => $card_id,
            'recharge_number' => makeOrderNo(),
            'addtime' => time(),
            'status' => 0,
        ];
        $res = db('recharge_card_order')->insert($data);
        if($res){
            return json(['code'=>1,'msg'=>"订单生成成功",'data'=>$data['recharge_number']]);
        }else{
            return json(['code'=>0,'msg'=>"订单生成是失败"]);
        }
        
    }

    
    
    //次卡生成订单 
    public function second()
    {
        
        $user_id = $this->user_id;
        $card_id = input('card_id');
        if(!$card_id) return json(['code'=>0,'msg'=>"参数错误"]);

        $card = db('second_card')->where(['id'=>$card_id,'status'=>0])->find();
        if(empty($card)) return json(['code'=>0,'msg'=>"该次卡已下架"]);
        if($card['number']<1) return json(['code'=>0,'msg'=>"次卡库存不足"]);
        $data = [
            'user_id' => $user_id,
            'money' => $card['money'],
            'card_id' => $card_id,
            'recharge_number' => makeOrderNo(),
            'addtime' => time(),
            'status' => 0,
        ];
        $res = db('second_card_order')->insert($data);
        if($res){
            return json(['code'=>1,'msg'=>"订单生成成功",'data'=>$data['recharge_number']]);
        }else{
            return json(['code'=>0,'msg'=>"订单生成是失败"]);
        }
        
    }
    
    
    //会员卡升级生成订单
    public function vip(){
        
        $user_id = $this->user_id;
        $card_id = input('card_id');
        if(!$card_id) return json(['code'=>0,'msg'=>"参数错误"]);

        $card = db('vip_cate')->where(['id'=>$card_id,'status'=>0])->find();
        if(empty($card)) return json(['code'=>0,'msg'=>"该会员卡已下架"]);
        //查找用户的会员等级
        $user_level = db('user')->where(['user_id'=>$user_id])->value('vip_id');
        if($card['level']<=$user_level){
            return json(['code'=>0,'msg'=>"请购买更高等级的会员卡"]);
        }

        $data = [
            'user_id' => $user_id,
            'money' => $card['money'],
            'card_id' => $card_id,
            'old_level' => $user_level,
            'new_level' => $card['level'],
            'money' => $card['need_money'],
            'upgrade_number' => makeOrderNo(),
            'addtime' => time(),
            'status' => 0,
        ];
        $res = db('vip_upgrade')->insert($data);
        if($res){
            return json(['code'=>1,'msg'=>"订单生成成功",'data'=>$data['upgrade_number']]);
        }else{
            return json(['code'=>0,'msg'=>"订单生成是失败"]);
        }
    }

}
